package com.zyw.zhuangdian.dao.impl;

import com.zyw.zhuangdian.dao.BaseDao;
import com.zyw.zhuangdian.dao.UserDao;
import com.zyw.zhuangdian.exceptions.MyDaoException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Repository
@Transactional
public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public void addUser(Map in) {
        int i = sqlTemplate.insert("UserDaoImpl.addUser",in);
        if (i<1){
            throw new MyDaoException("插入用户失败");
        }
    }

    @Override
    public void updateUser(Map in) {
        int i = sqlTemplate.update("UserDaoImpl.updateUser",in);
        if (i<1){
            throw new MyDaoException("更新用户失败");
        }
    }

    @Override
    public List<Map> listUsers(Map in) {
        List<Map> list = sqlTemplate.selectList("UserDaoImpl.listUsers",in);
        return list;
    }

    @Override
    public int listUsersCount(Map in) {
        List<Map> list = sqlTemplate.selectList("UserDaoImpl.listUsersCount",in);
        if (list.size() < 1){
            return 0;
        }
        return Integer.parseInt(list.get(0).get("count").toString());
    }

    @Override
    public List<Map> listUsersForLogin(Map in) {
        List<Map> list = sqlTemplate.selectList("UserDaoImpl.listUsersForLogin",in);
        return list;
    }

    @Override
    public int listUsersCountForLogin(Map in) {
        List<Map> list = sqlTemplate.selectList("UserDaoImpl.listUsersCountForLogin",in);
        if (list.size() < 1){
            return 0;
        }
        return Integer.parseInt(list.get(0).get("count").toString());
    }
}
